var fs = require("fs");
var _ = require("lodash");

let fileNameC = "Profession";
let fileName1 = "ProDes";
let fileName2 = "./data/skill4";
let fileName3 = "ChaPaDes";
let outputFileName = "./data/pro.json";

let mainData = JSON.parse(fs.readFileSync(`${fileNameC}.json`).toString()).map;
let proDes = JSON.parse(fs.readFileSync(`${fileName1}.json`).toString()).map;
let skillDes = JSON.parse(fs.readFileSync(`${fileName2}.json`).toString());
let chaPa = JSON.parse(fs.readFileSync(`${fileName3}.json`).toString()).map;

let proMap = new Map();
let chaPaMap = new Map();
let skillMap = new Map();
_.forEach(proDes, (e) => {
  let id = e.id;
  let data = e.data;
  proMap.set(id, data);
});
_.forEach(skillDes, (e) => {
  let id = e.id;
  let data = e.data;
  skillMap.set(id, data);
});
_.forEach(chaPa, (e) => {
  let id = e.id;
  let data = e.data;
  chaPaMap.set(id, data);
});
_.forEach(mainData, (e) => {
  _.forEach(e.data.linkSkill, (x) => {
    if (skillMap.has(x.skillId)) {
      _.assign(x, skillMap.get(x.skillId));
    }
  });
  _.forEach(e.data.properSkill, (x) => {
    if (skillMap.has(x.skillId)) {
      _.assign(x, skillMap.get(x.skillId));
    }
  });
  _.forEach(e.data.commonSkill, (x) => {
    if (skillMap.has(x.skillId)) {
      _.assign(x, skillMap.get(x.skillId));
    }
  });
  _.forEach(e.data.seriesSkill, (x) => {
    if (skillMap.has(x.skillId)) {
      _.assign(x, skillMap.get(x.skillId));
    }
  });
  _.forEach(e.data.paSkill, (x) => {
    if (chaPaMap.has(x.skillId)) {
      _.assign(x, chaPaMap.get(x.skillId));
    }
  });
  let id = e.data.id;
  if (proMap.has(id)) {
    _.assign(e.data, proMap.get(id));
  }
});

fs.writeFileSync(outputFileName, JSON.stringify(mainData));
